<?php
namespace app\common\logic\ad;


use app\common\model\ad\Ads;
use app\common\model\ad\Rule;
use think\Db;
use think\exception\ValidateException;
use think\View;

/**
 * Created by PhpStorm.
 * User: yangchunfu
 * Date: 2019/12/24
 * Time: 4:36 PM
 */

abstract class Driver
{
    protected $options = [];
    //模版路径
    protected $viewDir = __DIR__ . DS . 'rulehtml' . DS;
    protected $htmlTpl = '';

    //模版对象
    protected $view;
    protected $adModel;       //广告模型
    protected $ruleModel;    //rule 模型
    public static $curRule;  //当前规则
    protected $rule = [];
    protected $ruleWhere = [];   //规则默认条件
    public function __construct($options)
    {
        $this->view = new View();
        $this->options = array_merge($this->options, $options);

        self::$curRule = $this->options['rule'];
        $this->adModel = new Ads();
        $this->ruleModel = new Rule();
    }






    /**
     * 获取规则html
     * @param int $ads_id
     * @return string
     */
    public function insertRule($ads_id = null) {
        return '';
    }

    /**
     * 获取某条数据的规则和数据
     * @param mixed $buildparams fastadmin生成的查询数据
     * @param mixed $rule 要添加的数据
     * @throws Exception
     * @return mixed
     */

    public function first($buildparams = [])
    {
        $this->checkRule();
        list($where, $sort, $order, $offset, $limit) = $buildparams;
        $total = $this->adModel
            ->where('id','IN',function($query) {
                $query->name('self_ad_rule')->where($this->getRule())->field('ad_ads_id');
            })
            ->where($where)
            ->order($sort, $order)
            ->count();
        $list = $this->adModel
            ->where('id','IN',function($query) {
                $query->name('self_ad_rule')->where($this->getRule())->field('ad_ads_id');
            })
            ->where($where)->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $list = collection($list)->toArray();
        $result = array("total" => $total, "rows" => $list);

        return $result;
    }

    /**
     * 获取规则
     * @return array
     */
    public function getRule() {
        $rule = array_merge($this->ruleWhere, ['rule'=> self::$curRule]);

        return $rule;
    }

    /**
     * 检测规则
     * @return boolean
     */
    public function checkRule() {
        if (!in_array(self::$curRule, $this->rule)) {
            throw new ValidateException('规则条件传递不对');
        }

        return true;
    }


    /**
     * 添加广告
     * @param array $data 要添加的数据
     * @return mixed
     */
    public function add($data = []) {
        Db::startTrans();
        try {
            $rules = $this->options;
            $this->adModel->data($data);
            $this->adModel->allowField(true)->save();
            $rules['ad_ads_id'] = $this->adModel->id;
            $this->ruleModel->data($rules)->save();
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new ValidateException($e->getMessage());
        }
    }

    /**
     * 修改广告
     * @param array $data 要添加的数据
     * @param object $row 要添加的数据
     * @return mixed
     */
    public function edit(Ads $row, $data = []) {
        $row->allowField(true)->save($data);
    }


    /**********************************接口部分*/

    /**
     * 获取广告 接口 api
     * @return array
     */
    public function select($where = []){

        //获取广告数据
        $ad = $this->adModel->where('id','IN',function($query) {
            $query->name('self_ad_rule')->where($this->getRule())->field('ad_ads_id');
        })->where($where)->paginate()->each(function($item, $key) {

        });

        return $ad;
    }


}